package com.ftg.learn.bus.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.bus.VO.busExCostAmountVo;
import com.ftg.learn.bus.VO.busExFundsVo;
import com.ftg.learn.bus.VO.busExUpdateVo;
import com.ftg.learn.bus.entity.Approval;
import com.ftg.learn.bus.entity.BusExpenditure;
import com.ftg.learn.bus.VO.FundVo;
import com.ftg.learn.bus.entity.Fund;
import com.ftg.learn.bus.entity.request.InsertJFZCRequest;
import com.ftg.learn.bus.mapper.ApprovalMapper;
import com.ftg.learn.bus.mapper.BusExpenditureMapper;
import com.ftg.learn.bus.service.IBusExpenditureService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 公车经费支出登记表 服务实现类
 * </p>
 *
 * @author lalala
 * @since 2022-03-07
 */
@Service
public class BusExpenditureServiceImpl extends ServiceImpl<BusExpenditureMapper, BusExpenditure> implements IBusExpenditureService {

    @Resource
    private BusExpenditureMapper busExpenditureMapper;

    @Resource
    private ApprovalMapper approvalMapper;

    @Autowired
    protected Mapper dozerMapper;

    /**
     * 添加公车经费支出登记表所有信息
     *
     * @return
     */
    @Transactional(rollbackFor = SQLException.class)
    @Override
    public boolean insertInfo(InsertJFZCRequest insertAutoRequest) {
        // VO类映射进来了 https://blog.csdn.net/hanshanyunhai/article/details/108448259
//        BusExpenditure busExpenditure = dozerMapper.map(insertAutoRequest, BusExpenditure.class);
        BusExpenditure busExpenditure = BusExpenditure.builder().burid(insertAutoRequest.getBurid())
                .receiverDept(insertAutoRequest.getReceiverDept())
                .remarks(insertAutoRequest.getRemarks()).build();
        Map<String, Object> map = busExpenditureMapper.selectAutoInfo(insertAutoRequest.getBurid());
        String is_special = busExpenditureMapper.selectExStatus(insertAutoRequest.getBurid());
        Long a = Long.valueOf(String.valueOf(map.get("burid")));
        String b = String.valueOf(map.get("bus_no"));
        String c = String.valueOf(map.get("dname"));
        String d = String.valueOf(map.get("ins_equ_no"));
        String e = String.valueOf(map.get("receiver"));
        int examine_status = 0;
        if ("1".equals(is_special)) {
            examine_status = 1;
        }
        busExpenditure.setBurid(a);
        busExpenditure.setBusNo(b);
        busExpenditure.setDname(c);
        busExpenditure.setInsEquNo(d);
        busExpenditure.setReceiver(e);
        busExpenditure.setExamineStatus(examine_status);
        busExpenditureMapper.insert(busExpenditure);
        Long beid = busExpenditure.getBeid();

        List<FundVo> fundVoList = insertAutoRequest.getFundVos();
        // 判断这次出车花没花钱
        if (Objects.nonNull(fundVoList)){
            // 经费表的添加
            for (FundVo fundVo : fundVoList) {
                busExpenditureMapper.insertFund(beid, fundVo.getPid(),
                        busExpenditureMapper.selectPname(fundVo.getPid()),
                        fundVo.getCost_amount(),
                        fundVo.getInvoice_no(),
                        fundVo.getManufacturer());
            }
        }
        // 是否生成审批
        if (busExpenditure.getExamineStatus() == 1) {
            Approval approval = new Approval();
            approval.setBeid(beid);
            approval.setUaid(-1L);
            approval.setAname("待填写");
            approval.setAphone("待填写");
            approval.setIspassed(1);
            approval.setRemarks("待填写");
            approvalMapper.insert(approval);
        }
        return true;
    }

    /**
     * 单纯的修改经费支出表内的信息。
     * @param insertAutoRequest
     * @return
     */
    @Override
    public boolean updateExFund1(busExUpdateVo insertAutoRequest){
        BusExpenditure busExpenditure = BusExpenditure.builder().burid(insertAutoRequest.getBurid())
                .receiverDept(insertAutoRequest.getReceiverDept())
                .remarks(insertAutoRequest.getRemarks()).build();
        Map<String, Object> map = busExpenditureMapper.selectAutoInfo(insertAutoRequest.getBurid());
        Long a = Long.valueOf(String.valueOf(map.get("burid")));
        String b = String.valueOf(map.get("bus_no"));
        String c = String.valueOf(map.get("dname"));
        String d = String.valueOf(map.get("ins_equ_no"));
        String e = String.valueOf(map.get("receiver"));
        busExpenditure.setBeid(insertAutoRequest.getBeid());
        busExpenditure.setBurid(a);
        busExpenditure.setBusNo(b);
        busExpenditure.setDname(c);
        busExpenditure.setInsEquNo(d);
        busExpenditure.setReceiver(e);
        busExpenditure.setUpdateTime(LocalDateTime.now());
        busExpenditureMapper.updateById(busExpenditure);
        return true;
    }

    /**
     * 删除经费支出表数据，同时删除支出表内和审批表内关联数据
     * @param beid
     * @return
     */
    @Override
    @Transactional(rollbackFor = SQLException.class)
    public boolean delBusEx(Long beid) {
        this.removeById(beid);
        busExpenditureMapper.deleteApproval(beid);
        busExpenditureMapper.deleteFund(beid);
        return true;
    }


    /**
     * 修改经费支出表，以及删除经费表并重新添加，达到修改经费表的目的。
     * @param busExFundsVo busExFundsVo
     * @return
     */
    @Transactional(rollbackFor = SQLException.class)
    @Override
    public boolean updateExFund(busExFundsVo busExFundsVo){
        BusExpenditure busExpenditure = BusExpenditure.builder()
                .beid(busExFundsVo.getBeid())
                .burid(busExFundsVo.getBurid())
                .busNo(busExFundsVo.getBusNo())
                .dname(busExFundsVo.getDname())
                .examineStatus(busExFundsVo.getExamineStatus())
                .insEquNo(busExFundsVo.getInsEquNo())
                .receiver(busExFundsVo.getReceiver())
                .receiverDept(busExFundsVo.getReceiverDept())
                .remarks(busExFundsVo.getRemarks())
                .build();
        busExpenditure.setUpdateTime(LocalDateTime.now());
        busExpenditureMapper.updateById(busExpenditure);
        busExpenditureMapper.deleteFund(busExpenditure.getBeid());
        List<FundVo> fundVoList = busExFundsVo.getFundVos();
        // 判断这次修改是否把经费都取消
        if (Objects.nonNull(fundVoList)){
            // 经费表的重新添加
            for (FundVo fundVo : fundVoList) {
                busExpenditureMapper.insertFund(busExpenditure.getBeid(), fundVo.getPid(),
                        busExpenditureMapper.selectPname(fundVo.getPid()),
                        fundVo.getCost_amount(),
                        fundVo.getInvoice_no(),
                        fundVo.getManufacturer());
            }
        }
        return true;
    }

    /**
     * 分页查询经费支出表全部数据加上对应beid的经费表经费支出总和。
     * @param pagination
     * @return
     */
    @Override
    public List<busExCostAmountVo> showBusExAndSumCost(IPage<busExCostAmountVo> pagination) {
        return busExpenditureMapper.showBusExAndSumCost(pagination);
    }

    /**
     * 按负责人姓名模糊查找页查询经费支出表全部数据加上对应beid的经费表经费支出总和。
     * @param pagination
     * @param dname
     * @return
     */
    @Override
    public List<busExCostAmountVo> showBusExAndSumCostByDname(IPage<busExCostAmountVo> pagination, String dname) {
        return busExpenditureMapper.showBusExAndSumCostByDname(pagination,dname);
    }

    /**
     * 根据此经费支出表id查询此次支出表详情。
     * @param beid
     * @return
     */
    @Override
    public List<Fund> showFund(Long beid) {
        return busExpenditureMapper.showFund(beid);
    }


    /**
     * 列出参数表中所有的经费类型
     * @return
     */
    @Override
    public List<Map<String, Object>> showCostType() {
        return busExpenditureMapper.showCostType();
    }


    /**
     * 列出参数表中所有的厂商。
     * @return
     */
    @Override
    public List<Map<String, Object>> showManufacturer() {
        return busExpenditureMapper.showManufacturer();
    }


    /**
     * 在审批表内查询经费支出表全部数据加上对应beid的经费表经费支出总和。
     * @param beid
     * @return
     */
    @Override
    public List<busExCostAmountVo> showBusExAndSumCostInApp(Long beid){
        return busExpenditureMapper.showBusExAndSumCostInApp(beid);
    }




}
